﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace graphics
{
    public partial class UserControlcut : UserControl
    {
        private Point startpoint;
        private Point oldpoint;
        private Rectangle therectangle;
        private Bitmap bb;
        private bool isdraw;
        private Graphics g;
        public string img_fullname;
        private string p;

        public UserControlcut()
        {
            InitializeComponent();
            //this.Width = 800;
            //this.Height = 600;
            g = this.CreateGraphics();
        }

        public UserControlcut(string p)
        {
            // TODO: Complete member initialization
            this.img_fullname = p;

            //string img_fullname = @"D:\all\常用资料\photo\2016\IMG_1944.JPG";
            bb = new System.Drawing.Bitmap(img_fullname);
            InitializeComponent();
        }

        private void UserControlpintu_Load(object sender, EventArgs e)
        {
            //bb = (Bitmap)bb.GetThumbnailImage(this.pictureBox1.Width, this.pictureBox1.Height, null, IntPtr.Zero);
            //bb = (Bitmap)bb.GetThumbnailImage(600, 600 ,null, IntPtr.Zero);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bb);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(bb, new Rectangle(0, 0, 400, 300), 0, 0, bb.Width, bb.Height, GraphicsUnit.Pixel);
            this.pictureBox1.Width = 400;
            this.pictureBox1.Height = 300;
            this.pictureBox2.Width = this.pictureBox1.Width;
            this.pictureBox2.Height = this.pictureBox1.Height;

            this.pictureBox1.Image = (Image)bb;
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            isdraw = true;
            if (e.Button == MouseButtons.Left)
            {
                startpoint = new Point(e.X, e.Y);
                oldpoint = new Point(e.X, e.Y);
            }
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isdraw)
            {
                //Graphics g = this.pictureBox1.CreateGraphics();
                //g.DrawRectangle(new Pen(Color.Black, 5), startpoint.X, startpoint.Y, e.X - startpoint.X, e.Y - startpoint.Y);
            }
        }

        /// <summary>
        /// 切图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isdraw = false;
            therectangle = new Rectangle(startpoint.X, startpoint.Y, e.X - startpoint.X, e.Y - startpoint.Y);

            if (e.X - startpoint.X != 0 && e.Y - startpoint.Y != 0)
            {
                if (this.pictureBox2.Image != null)
                    this.pictureBox2.Image.Dispose();
                Bitmap clonebitmap = bb.Clone(therectangle, PixelFormat.DontCare);

                this.pictureBox2.Image = clonebitmap;
            }
        }

        private void UserControlcut_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
            {
                string strFileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(":", "");
                string filename = strFileName + ".jpg";
                this.pictureBox2.Image.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), filename), ImageFormat.Jpeg);
                e.SuppressKeyPress = true; // don't pass the event down
            }
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
        }
    }
}